
const Koa = require('koa');
const fs = require('fs');
const app = new Koa();
const request = require('koa/lib/request');
const path = require('path')
var mysql = require('mysql');
const router = require("koa-router")();
const query = require("../mysql/query"); //引入异步查询方法

//文章发布
var publish = async (ctx, next) => {
  let title = ctx.request.body.title;
  let des = ctx.request.body.des;
  let content = ctx.request.body.content || '';
  let time = new Date().getTime();
  var  addSql = 'INSERT INTO articleList(title,des,content,time) VALUES(?,?,?,?)';
  var  addSqlParams = [title,des,content,time];
  await query(addSql,addSqlParams)
  ctx.body = {
    code:'200',
    msg:'成功'
  }
};

//文章列表
var articleList = async (ctx, next) => {
  var  Sql = `SELECT * FROM articleList`
  let res = await query(Sql)
  ctx.body = {
    data:res,
    code:'200',
    msg:'成功'
  }
};

//文章详情
var articleDetail = async (ctx, next) => {
  let id = ctx.request.body.id;
  var  Sql = `SELECT * FROM articleList WHERE id='${id}'`
  let res = await query(Sql)
  ctx.body = {
    data:res,
    code:'200',
    msg:'成功'
  }
};


module.exports = {
  'POST /publish': publish,
  'POST /articleList': articleList,
  'POST /articleDetail': articleDetail,
  
};